<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>gozerlib.plugs.sort &mdash; GOZERWAVE v0.1 BETA documentation</title>
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1 BETA',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="GOZERWAVE v0.1 BETA documentation" href="../../index.html" />
    <link rel="next" title="gozerlib.plugs.tail" href="tail.html" />
    <link rel="prev" title="gozerlib.plugs.reverse" href="reverse.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="tail.html" title="gozerlib.plugs.tail"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="reverse.html" title="gozerlib.plugs.reverse"
             accesskey="P">previous</a> |</li>
        <li><a href="../../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-gozerlib.plugs.sort">
<h1>gozerlib.plugs.sort<a class="headerlink" href="#module-gozerlib.plugs.sort" title="Permalink to this headline">¶</a></h1>
<p>sort bot results.</p>
<dl class="exception">
<dt id="gozerlib.plugs.sort.SortError">
<em class="property">exception </em><tt class="descclassname">gozerlib.plugs.sort.</tt><tt class="descname">SortError</tt><a class="headerlink" href="#gozerlib.plugs.sort.SortError" title="Permalink to this definition">¶</a></dt>
<dd>Bases: <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/exceptions.html#exceptions.Exception"><tt class="xref docutils literal"><span class="pre">exceptions.Exception</span></tt></a></dd></dl>

<dl class="class">
<dt id="gozerlib.plugs.sort.SortOptionParser">
<em class="property">class </em><tt class="descclassname">gozerlib.plugs.sort.</tt><tt class="descname">SortOptionParser</tt><a class="headerlink" href="#gozerlib.plugs.sort.SortOptionParser" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a title="(in Python v2.7)" class="reference external" href="http://docs.python.org/dev/library/optparse.html#optparse.OptionParser"><tt class="xref docutils literal"><span class="pre">optparse.OptionParser</span></tt></a></p>
<p>options parsers for the sort command.</p>
<dl class="method">
<dt id="gozerlib.plugs.sort.SortOptionParser.error">
<tt class="descname">error</tt><big>(</big><em>msg</em><big>)</big><a class="headerlink" href="#gozerlib.plugs.sort.SortOptionParser.error" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="gozerlib.plugs.sort.SortOptionParser.exit">
<tt class="descname">exit</tt><big>(</big><em>status=0</em>, <em>msg=None</em><big>)</big><a class="headerlink" href="#gozerlib.plugs.sort.SortOptionParser.exit" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="gozerlib.plugs.sort.SortOptionParser.format_help">
<tt class="descname">format_help</tt><big>(</big><em>formatter=None</em><big>)</big><a class="headerlink" href="#gozerlib.plugs.sort.SortOptionParser.format_help" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</dd></dl>

<dl class="function">
<dt id="gozerlib.plugs.sort.handle_sort">
<tt class="descclassname">gozerlib.plugs.sort.</tt><tt class="descname">handle_sort</tt><big>(</big><em>bot</em>, <em>ievent</em><big>)</big><a class="headerlink" href="#gozerlib.plugs.sort.handle_sort" title="Permalink to this definition">¶</a></dt>
<dd>sort the result list.</dd></dl>

<dl class="function">
<dt id="gozerlib.plugs.sort.numeric_compare">
<tt class="descclassname">gozerlib.plugs.sort.</tt><tt class="descname">numeric_compare</tt><big>(</big><em>x</em>, <em>y</em><big>)</big><a class="headerlink" href="#gozerlib.plugs.sort.numeric_compare" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<div class="section" id="code">
<h2>code<a class="headerlink" href="#code" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108</pre></td><td class="code"><div class="highlight"><pre><span class="c"># plugs/sort.py</span>
<span class="c">#</span>
<span class="c"># Sorting</span>

<span class="sd">&quot;&quot;&quot; sort bot results. &quot;&quot;&quot;</span>

<span class="n">__author__</span> <span class="o">=</span> <span class="s">&quot;Wijnand &#39;maze&#39; Modderman &lt;http://tehmaze.com&gt;&quot;</span>
<span class="n">__license__</span> <span class="o">=</span> <span class="s">&quot;BSD&quot;</span>

<span class="c">## gozerlib imports</span>

<span class="kn">from</span> <span class="nn">gozerlib.commands</span> <span class="kn">import</span> <span class="n">cmnds</span>
<span class="kn">from</span> <span class="nn">gozerlib.utils.generic</span> <span class="kn">import</span> <span class="n">waitforqueue</span>
<span class="kn">from</span> <span class="nn">gozerlib.examples</span> <span class="kn">import</span> <span class="n">examples</span>

<span class="c"># basic imports</span>
<span class="kn">import</span> <span class="nn">optparse</span>

<span class="k">class</span> <span class="nc">SortError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span> <span class="k">pass</span>

<span class="k">class</span> <span class="nc">SortOptionParser</span><span class="p">(</span><span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; options parsers for the sort command. &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-f&#39;</span><span class="p">,</span> <span class="s">&#39;--ignore-case&#39;</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s">&#39;fold lower case to upper case characters&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="n">action</span><span class="o">=</span><span class="s">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;ignorecase&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-n&#39;</span><span class="p">,</span> <span class="s">&#39;--numeric-sort&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s">&#39;compare according to string numerical value&#39;</span><span class="p">,</span> 
            <span class="n">action</span><span class="o">=</span><span class="s">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;numeric&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-r&#39;</span><span class="p">,</span> <span class="s">&#39;--reverse&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s">&#39;reverse the result of comparisons&#39;</span><span class="p">,</span> 
            <span class="n">action</span><span class="o">=</span><span class="s">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;reverse&#39;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-u&#39;</span><span class="p">,</span> <span class="s">&#39;--unique&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="n">help</span><span class="o">=</span><span class="s">&#39;output only the first of an equal run&#39;</span><span class="p">,</span> 
            <span class="n">action</span><span class="o">=</span><span class="s">&#39;store_true&#39;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&#39;unique&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">format_help</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">formatter</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">raise</span> <span class="n">SortError</span><span class="p">(</span><span class="s">&#39;sort [-fnru] [--ignore-case] [--numeric-sort] [--reverse] [--unique]&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">error</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">msg</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="n">msg</span><span class="o">=</span><span class="n">msg</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">exit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="mf">0</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">msg</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">SortError</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">SortError</span>

<span class="k">def</span> <span class="nf">numeric_compare</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
    <span class="k">try</span><span class="p">:</span> <span class="n">a</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
    <span class="k">except</span><span class="p">:</span> <span class="k">return</span> <span class="nb">cmp</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span> <span class="n">b</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
    <span class="k">except</span><span class="p">:</span> <span class="k">return</span> <span class="nb">cmp</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">a</span> <span class="o">-</span> <span class="n">b</span>

<span class="k">def</span> <span class="nf">handle_sort</span><span class="p">(</span><span class="n">bot</span><span class="p">,</span> <span class="n">ievent</span><span class="p">):</span>

    <span class="sd">&quot;&quot;&quot; sort the result list. &quot;&quot;&quot;</span>

    <span class="n">parser</span> <span class="o">=</span> <span class="n">SortOptionParser</span><span class="p">()</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">ievent</span><span class="o">.</span><span class="n">inqueue</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ievent</span><span class="o">.</span><span class="n">args</span><span class="p">:</span>
            <span class="n">ievent</span><span class="o">.</span><span class="n">missing</span><span class="p">(</span><span class="s">&#39;&lt;input&gt;&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">options</span><span class="p">,</span> <span class="n">result</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">ievent</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">SortError</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">ievent</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
            <span class="k">return</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="n">waitforqueue</span><span class="p">(</span><span class="n">ievent</span><span class="o">.</span><span class="n">inqueue</span><span class="p">,</span> <span class="mf">30</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">options</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">(</span><span class="n">ievent</span><span class="o">.</span><span class="n">rest</span><span class="o">.</span><span class="n">split</span><span class="p">())</span>
        <span class="k">except</span> <span class="n">SortError</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">ievent</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
            <span class="k">return</span>

    <span class="k">if</span> <span class="ow">not</span> <span class="n">result</span><span class="p">:</span>
        <span class="n">ievent</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="s">&#39;no data to sort&#39;</span><span class="p">)</span>
        <span class="k">return</span>

    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">unique</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">set</span><span class="p">(</span><span class="n">result</span><span class="p">))</span>

    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">numeric</span><span class="p">:</span>
        <span class="n">result</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">numeric_compare</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">result</span><span class="o">.</span><span class="n">sort</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">ignorecase</span><span class="p">:</span>
        <span class="n">result</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="k">lambda</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">cmp</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="n">b</span><span class="o">.</span><span class="n">upper</span><span class="p">()))</span>

    <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">reverse</span><span class="p">:</span>
        <span class="n">result</span><span class="o">.</span><span class="n">reverse</span><span class="p">()</span>
    
    <span class="n">ievent</span><span class="o">.</span><span class="n">reply</span><span class="p">(</span><span class="s">&quot;results: &quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">dot</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="n">cmnds</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">&#39;sort&#39;</span><span class="p">,</span> <span class="n">handle_sort</span><span class="p">,</span> <span class="p">[</span><span class="s">&#39;USER&#39;</span><span class="p">,</span> <span class="s">&#39;WEB&#39;</span><span class="p">,</span> <span class="s">&#39;CLOUD&#39;</span><span class="p">],</span> <span class="n">threaded</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">examples</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s">&#39;sort&#39;</span><span class="p">,</span> <span class="s">&#39;sort the output of a command&#39;</span><span class="p">,</span> <span class="s">&#39;list | sort&#39;</span><span class="p">)</span>
<span class="c">#tests.add(&#39;list | sort&#39;)</span>
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../../index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">gozerlib.plugs.sort</a><ul>
<li><a class="reference external" href="#code">code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="reverse.html"
                                  title="previous chapter">gozerlib.plugs.reverse</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="tail.html"
                                  title="next chapter">gozerlib.plugs.tail</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../../_sources/gozerlib/plugs/sort.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="tail.html" title="gozerlib.plugs.tail"
             >next</a> |</li>
        <li class="right" >
          <a href="reverse.html" title="gozerlib.plugs.reverse"
             >previous</a> |</li>
        <li><a href="../../index.html">GOZERWAVE v0.1 BETA documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2009, Bart Thate.
      Last updated on Nov 14, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>